var path = require('path')
var fs = require('fs');
var sql = require('../sql')
var upload = function(req, res) {
    res.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
    res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
    res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.setHeader("Access-Control-Allow-Credentials", "true");
    console.log(req.method)
    if (req.method == 'OPTIONS') {
        console.log('options 请求时，返回 200');

        // 返回结果
        res.writeHead(200, {
            'Content-type': 'text/html'
        });
        res.end('options OK');
        return;
    } else {
        var taskid = req.body.taskid;
        var projectid = req.body.projectid;

        var note = req.body.note;

        console.log(req)
        if (req.files.files == undefined) {
            var filename = req.files['wangEditorH5File'].originalFilename
            var path = req.files['wangEditorH5File'].path
        } else {
            var filename = req.files.files.originalFilename || path.basename(req.files.files.path);
            var path = req.files.files.path
        }

        var nowtime = new Date().getTime();
        var filenameArry = filename.split('.')
        var host = req.headers.host;
        var targetPath = '../files/' + filenameArry[0] + nowtime + '.' + filenameArry[1];
        // var targetPath = './' + filenameArry[0] + nowtime + '.' + filenameArry[1];


        console.log(targetPath)
        fs.createReadStream(path).pipe(fs.createWriteStream(targetPath));
        var type = filenameArry[1]
        if (type == 'jpg' || type == 'jpeg' || type == "png" || type == 'bnm') {
            if (note == 1) { //1代表是文件，2代表的是评论中的图片
                type = 1
            } else {
                type = 2
            }
        } else {
            type = 0
        }

        sql.File.create({
                'filepath': targetPath,
                'filename': filename,
                'taskid': taskid,
                'projectid': projectid,
                'type': type,
                'note': note
            }).then(function(file) {
                res.json(file)
            })
            // res.json({ code: 200, msg: { url: 'http://' + req.headers.host + '/' + filename } });
    }
}
var getFile = function(req, res) {
    var projectid = req.body.projectid;
    sql.File.findAll({
        'where': {
            'projectid': projectid
        }
    }).then(function(files) {
        res.json(files)
    })
}

var updateFile = function(req, res) {
    var idString = req.body.ids;
    var ids = idString.split(',')
    var commentid = req.body.commentid;
    sql.File.update({
        'commentid': commentid
    }, {
        'where': {
            'id': ids
        }
    }).then(function(data) {
        res.send(data)
        console.log(data)
    })
}
var deleteFile = function(req, res) {
    var id = req.body.id.split(',')
    sql.File.destroy({
        'where': {
            'id': id
        }
    }).then(function(result) {
        if (result) {
            res.json({ 'status': true, 'code': 1 })
        } else {
            res.json({ 'status': false, 'code': 0 })
        }
    })
}
module.exports = { upload, getFile, deleteFile, updateFile }